<?php
/**
 * Created by PhpStorm.
 * User: zhang
 * Date: 2018/4/29
 * Time: 15:55
 */
namespace app\account\controller;

use app\common\model\Base;



class Userinfo extends Base{

    public function __construct()
    {
        parent::__construct();
    }
    /**
     *
     * @api {get} /account/userinfo 用户信息
     * @apiDescription 获取用户信息
     * @apiGroup account
     * @apiParam {string} uid 用户的uid
     * @apiParam {string} action 方法
     *
     *@apiParamExample {json} Request-Example:
     *     {
     *       "tel": 17754928306，
     *       "passd": 123456789
     *     }
     *
     * @apiSuccess {String} code 0 表示失败 1 表示成功.
     * @apiSuccess {String} msg 登录的个人信息.
     * @apiSuccessExample {json} Success-Response:
     *     {
     *       "code": "1",
     *       "msg": "xxx"
     *     }
     */
    public function index(){
        if($this->isNull($_GET["uid"])){

        }
        $this->getinfo();
    }

    public function getinfo(){
        $uid = $_GET["uid"];
        $this->selectUserInfo($uid);
    }

    public function setinfo(){
        $uid = $_GET["uid"];
        $action = $_GET["action"];
        $value = $_GET["value"];

        if ($this->isSetNull($uid, $action, $value)) {
            $this->executeInsert($uid, $action, $value);
        }
    }

    /**
     *  判断账号或密码是否为空
     * @param str $uid 账号
     * @return boolean
     */
    function isNull($para) {
        if (empty($para)) {
            echo echoJson(0, "参数为空!");
            return false;
        }
        
        return true;
    }

    /**
     * 查询账号和密码是否匹配
     * @param str $tel 账号
     * @param str $passd 密码
     */
    function selectUserInfo($uid) {
        $sql = "SELECT * FROM user where uid='$uid'";
        $row = $this->mysql->selectSQLRow($sql);
        if ($row) {
            $this->getUserInfo($row);
        } else {
            echo echoJson(0, "账号不存在!");
        }
    }

    /**
     * 得到用户信息并输出
     * @param arr $row 查询到的用户信息
     * @param type $tel 账号
     */
    function getUserInfo($row) {
        isset($row["nickname"]) ? $nickname = $row["nickname"] : $nickname = null;
        isset($row["birthday"]) ? $birthday = $row["birthday"] : $birthday = null;
        isset($row["sex"]) ? $sex = $row["sex"] : $sex = null;
        isset($row["stature"]) ? $stature = $row["stature"] : $stature = null;
        isset($row["stride"]) ? $stride = $row["stride"] : $stride = null;
        isset($row["contact_tel"]) ? $contact_tel = $row["contact_tel"] : $contact_tel = null;
        isset($row["contact_Email"]) ? $contact_Email = $row["contact_Email"] : $contact_Email = null;
        isset($row["contact_qq"]) ? $contact_qq = $row["contact_qq"] : $contact_qq = null;
        isset($row["bonus_point"]) ? $integral = $row["bonus_point"] : $integral = null;
        isset($row["alipay_name"]) ? $alipay_name = $row["alipay_name"] : $integral = null;
        isset($row["alipay_account"]) ? $alipay_account = $row["alipay_account"] : $integral = null;
        $userInfo = array("nickname" => $nickname, "birthday" => $birthday,
            "sex" => $sex, "stature" => $stature, "stride" => $stride,
            "contact_tel" => $contact_tel, "contact_Email" => $contact_Email,
            "contact_qq" => $contact_qq,"integral"=>$integral,"alipay_name"=>$alipay_name,"alipay_account"=>$alipay_account);
        echo echoJson(1, $userInfo);
    }

    //设置用户信息
    function executeInsert($uid, $action, $value) {
        $sql = $this->getActionSQL($uid, $action, $value);
        if ($sql) {
            if ($this->mysql->insertSQL($sql, '添加失败，插入数据出错!')) {
                echo echoJson(1, "设置成功!");
            } else {
                echo echoJson(0, "headpic失败，请稍后再试!");
            }
        } else {
            echo echoJson(0, "SQL为空!");
        }
    }

    /**
     *  判断账号是否为空
     * @param str $uid 账号
     * @param int $action 插入的操作类型（1代表：插入体重，2代表：插入血压（血压分为舒张压和收缩压，$value代表舒张压，$value2代表收缩压），3代表：插入心率）
     * @param str $value 值（体重、舒张压或心率）
     *
     * @return boolean
     */
    function isSetNull($uid, $action, $value) {
        if (empty($uid) || $uid == "undefined") {
            echo echoJson(0, "添加失败，账号为空！");
            return false;
        }if (!isset($value)) {
            echo echoJson(0, "添加失败，值为空!");
            return false;
        }
        if (empty($action) || !is_numeric($action) || $action > 9 || $action < 1) {
            echo echoJson(0, "添加失败，操作错误或为空!");
            return false;
        }
        return true;
    }

    /**
     *  得到操作的sql语句
     * @param str $uid 账号
     */
    function getActionSQL($uid, $action, $value) {
        switch ($action) {
            case '1'://更新头像
                return " UPDATE user SET headpic='$value' WHERE uid='$uid'";
            case '2'://更新昵称
                return " UPDATE user SET nickname='$value' WHERE uid='$uid'";
            case '3'://更新性别
                return " UPDATE user SET sex='$value' WHERE uid='$uid'";
            case '4'://更新出生日期
                return " UPDATE user SET birthday='$value' WHERE uid='$uid'";
            case '5'://更新联系电话
                return " UPDATE user SET contact_tel='$value' WHERE uid='$uid'";
            case '6'://更新邮箱
                return " UPDATE user SET contact_Email='$value' WHERE uid='$uid'";
            case '7'://更新QQ号
                return " UPDATE user SET contact_qq='$value' WHERE uid='$uid'";

        }
    }
}